package gdou.gym.web.user;

import com.github.pagehelper.PageInfo;
import gdou.gym.entity.Eqment;
import gdou.gym.entity.SiteApplication;
import gdou.gym.entity.SpaceView;
import gdou.gym.entity.User;
import gdou.gym.exception.StateCode;
import gdou.gym.exception.SysException;
import gdou.gym.returnvo.UserQueryInfoVo;
import gdou.gym.service.equipment.EqService;
import gdou.gym.service.match.MatchService;
import gdou.gym.service.order.OrderService;
import gdou.gym.service.space.SpaceService;
import gdou.gym.service.user.UserService;
import gdou.gym.sys.order.Remain;
import gdou.gym.token.AllowAccessFor;
import gdou.gym.token.Authorization;
import gdou.gym.token.TokenUtil;
import gdou.gym.vo.MyPageInfo;
import gdou.gym.vo.OrderViewVo;
import gdou.gym.vo.Page;
import gdou.gym.vo.SpaceAndTimeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private SpaceService spaceService;
    @Resource
    private OrderService orderService;

    @Resource
    private EqService eqService;
    @Resource
    private MatchService matchService;


    @Resource
    private Remain remain;

    @GetMapping("/init")
    public Map<String, List<String>> init() {
        List<String> time = spaceService.getSiteTime();
        List<String> type = spaceService.getSiteType();
        Map<String, List<String>> map = new HashMap<>();
        map.put("time", time);
        map.put("type", type);
        return map;
    }

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody User u, HttpSession session) {
        User user = userService.login(u);
        Map<String, Object> map = new HashMap<>();
        String token = TokenUtil.getToken(user);
        map.put("token", token);
        map.put("account", user.getAccount());
        map.put("name", user.getName());
        map.put("type", user.getType());
        return map;
    }


    @GetMapping("/search")
    @AllowAccessFor({Authorization.STAFF, Authorization.STUDENT})
    public PageInfo<SpaceView> search(@Page(clazz = UserQueryInfoVo.class)
                                              MyPageInfo<UserQueryInfoVo> page) {
        List<SpaceView> space = spaceService.queryByCondition(page, false);
        return new PageInfo<>(space);

    }

    @GetMapping("/test")
    public Object test() {
        return remain.getRemainMap();
    }


    @GetMapping("/order")
    @AllowAccessFor({Authorization.STAFF, Authorization.STUDENT})
    public void order(Integer spaceTimeID, @RequestHeader("token") String token) {
        String account = TokenUtil.getAccount(token);
        orderService.addOrder(spaceTimeID, account);
    }

    @GetMapping("/orderDetail")
    @AllowAccessFor({Authorization.STAFF, Authorization.STUDENT})
    public List<OrderViewVo> queryMyOrderMsg(@RequestHeader("token") String token) {
        return orderService.queryMyOrderMsg(TokenUtil.getAccount(token));
    }

    @GetMapping("/cancel")
    public void cancelsOrder(String orderNo) {
        orderService.cancelOrder(orderNo);
    }

    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody User user) {
        userService.register(user);
        Map<String, Object> map = new HashMap<>();
        map.put("token", TokenUtil.getToken(user));
        return map;
    }

    @GetMapping("/myask4")
    public PageInfo<SiteApplication> queryMyAsk(@RequestHeader String token) {
        return spaceService.queryMyAsk(TokenUtil.getAccount(token));
    }

    @GetMapping("/eqrent")
    public List<?> queryMyRent(@RequestHeader String token) {
        return eqService.queryMyRent(TokenUtil.getAccount(token));
    }


    @PostMapping("/rent")
    public void addRent(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        String account = TokenUtil.getAccount(token);
        eqService.addEqAsk(map, account);
    }


    /*以下暂未实现*/
    @GetMapping("/ask4Match")
    public void ask4Match(Integer nums) {
        SpaceAndTimeVo spaceAndTimeVo = new SpaceAndTimeVo();
        Eqment eqment = new Eqment();
        matchService.ask4Match(spaceAndTimeVo, eqment, nums);
    }

}
